package cn.itcast.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.stereotype.Controller;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class JwtUtil {
/**
 * 过期十五分钟
 */
	private static final long EXPIRE_TIME = 15*60*1000;
/**
 * 
 */
	private static final String TOKEN_SECRET=UUID.randomUUID().toString();
/**
 * 生成签名，15min过期
 * 
 * @param username 用户名
 * @return 加密的token
 */
	public static String sign(String username,Integer userId) {
		try {
			//过期时间
			Date date = new Date(System.currentTimeMillis()+EXPIRE_TIME);
			//私钥及加密算法
			Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
			//设置头部信息
			Map<String,Object> header = new HashMap<>(2);
			header.put("typ", "JWT");
			header.put("alg", "HS256");
			//附带username，userId信息,生成签名
			return JWT.create()
					.withHeader(header)
					.withClaim("loginName", username)
					.withClaim("userId", userId)
					.withExpiresAt(date)
					.sign(algorithm);
		}catch(UnsupportedEncodingException e) {
			return null;
		}
	}
	/**
	 * 校验token是否正确
	 * @param token 密钥
	 * @return 是否正确
	 */
public static boolean verify(String token) {
	try {
		Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
		JWTVerifier verifier = JWT.require(algorithm).build();
		DecodedJWT jwt = verifier.verify(token);
		return true;
	}catch (Exception exception) {
		return false;
	}
}
}
